<?php

namespace app\api\controller\jumpjump;

use app\admin\model\JumpjumpAcc;
use app\admin\model\JumpjumpShare;
use app\common\controller\Api;
use think\Log;
use think\Request;

/**
 * 蹦蹦jump任务相关接口
 */
class Task extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    // 根据查询条件获取所有任务信息
    public function get_taskitem_info($field)
    {
        $data = array();
        $share = JumpjumpShare::where($field)->column('position1, position2, position3, position4, position5, position6');
        foreach ($share as $key => $value) {
            foreach ($value as $v) {
                array_push($data, $v);
            }
        }
        return $data;
    }

    //判断任务最后更新时间是否是今天,进行操作
    public function checkTaskTime($field)
    {
        $share = JumpjumpShare::where($field)->find();
        $updatetime = $share->updatetime;
        $begintoday= mktime(0,0,0,date('m'),date('d'),date('Y'));
        if ($updatetime < $begintoday) {
            $share->position1 = '';
            $share->position2 = '';
            $share->position3 = '';
            $share->position4 = '';
            $share->position5 = '';
            $share->position6 = '';
        }
        $share->save();
    }

    // 获取任务数据
    public function get_task_info($openid)
    {
        if ($share = JumpjumpShare::get(['fromUser' => $openid])) {
            $this->checkTaskTime(['fromUser' => $openid]);
            $data = $this->get_taskitem_info(['fromUser' => $openid]);
        } else {
            $s = new JumpjumpShare;
            $s->fromUser = $openid;
            $s->save();
            $data = array('', '', '', '', '', '');
        }

        $this->result('ok', $data);
    }

    // 获取任务奖励接口
    public function get_task_money($openid, $position)
    {
        $acc = JumpjumpAcc::get(['openid' => $openid]);
        // 更新金钱
        $acc->money += 200;

        $itemlist = $this->get_taskitem_info(['fromUser' => $openid]);
        // 判断机制
        foreach ($itemlist as $key => $item) {
            if ($key == $position) {
                if ($item) {
                    if ($item == 2) $this->error('重复领取奖励');
                    $itemlist[$key] = 2;
                } else {
                    $this->error('任务位置错误');
                }
            }
        }

        // 设置要更新的数据并更新
        $task = array();
        foreach ($itemlist as $key => $item) {
            $i = ['position' . (intval($key) + 1) => $item];
            $task = array_merge($task, $i);
        }

        $share = JumpjumpShare::get(['fromUser' => $openid]);
        $share->save($task);
        $acc->save();

        $data['task'] = $itemlist;
        $data['money'] = $acc->money;

        $this->result('ok', $data);
    }

    // 获取任务完成接口
    public function get_task_complete(Request $request)
    {
        $fromUser = $request->post('fromUser'); // 来源用户
        $toUser = $request->post('toUser'); // 被邀请用户
        $position = $request->post('position'); // 任务定位

        $itemlist = $this->get_taskitem_info(['fromUser' => $fromUser]);

        // 判断机制
        foreach ($itemlist as $key => $item) {
            if ($item == $toUser) $this->error('重复邀请用户');
            if ($key == $position) {
                if ($item == 2) $this->error('重复领取奖励');
                $itemlist[$key] = $toUser;
            }
        }

        // 设置要更新的数据并更新
        $data = array();
        foreach ($itemlist as $key => $item) {
            $i = ['position' . (intval($key) + 1) => $item];
            $data = array_merge($data, $i);
        }

        $share = JumpjumpShare::get(['fromUser' => $fromUser]);
        $share->save($data);

        $this->success('ok');
    }
}